//
//  Utile.m
//  JmzGo
//
//  Created by qq q on 16/1/28.
//  Copyright © 2016年 金拇指. All rights reserved.
//

#import "Utile.h"

@implementation Utile

/**
 *  静态的Util对象
 */
static Utile *utile = nil;

/**
 *  Util类的单例
 *
 *  @return 返回Util的对象
 */
+(id)shareInstance
{
    @synchronized(self){
        if (nil == utile) {
            utile = [[Utile alloc] init];
        }
        
        return utile;
    }
    
}

/**
 *  弹出提示框
 *
 *  @param view 弹出的视图View
 *  @param text 显示的文字
 */
+ (void)showToast:(UIView *)view withText:(NSString *)text
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    //    hud.labelText = text;
    hud.detailsLabel.text = text;
    [hud showAnimated:YES whileExecutingBlock:^{
        sleep(2);
    } completionBlock:^{
        [hud removeFromSuperview];
    }];
    
}

/**
 *  利用正则表达式解析URL
 *
 *  @param CS         参数名称
 *  @param webaddress 要解析的URL
 *
 *  @return 返回解析得到的参数
 */
+(NSString *)analysisURLParam:(NSString *)CS webaddress:(NSString *)webaddress
{
    NSError *error;
    NSString *regTags=[[NSString alloc] initWithFormat:@"(^|&|\\?)+%@=+([^&|^#]*)(&|$|#)",CS];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    
    // 执行匹配的过程
    NSArray *matches = [regex matchesInString:webaddress
                                      options:0
                                        range:NSMakeRange(0, [webaddress length])];
    
    for (NSTextCheckingResult *match in matches) {
        
        NSString *tagValue = [webaddress substringWithRange:[match rangeAtIndex:2]];
        
        return tagValue;
    }
    
    return @"";
}

+ (NSString *)getParamFromURL:(NSString *)param webaddress:(NSString *)url
{
    NSError *error;
    NSString *regTags=[[NSString alloc] initWithFormat:@"(^|&|\\?)+%@=+([^&]*)(&|$)",param];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    
    // 执行匹配的过程
    NSArray *matches = [regex matchesInString:url
                                      options:0
                                        range:NSMakeRange(0, [url length])];
    for (NSTextCheckingResult *match in matches) {
        //NSRange matchRange = [match range];
        //NSString *tagString = [webaddress substringWithRange:matchRange];  // 整个匹配串
        //        NSRange r1 = [match rangeAtIndex:1];
        //        if (!NSEqualRanges(r1, NSMakeRange(NSNotFound, 0))) {    // 由时分组1可能没有找到相应的匹配，用这种办法来判断
        //            //NSString *tagName = [webaddress substringWithRange:r1];  // 分组1所对应的串
        //            return @"";
        //        }
        
        NSString *tagValue = [url substringWithRange:[match rangeAtIndex:2]];  // 分组2所对应的串
        //    NSLog(@"分组2所对应的串:%@\n",tagValue);
        return tagValue;
    }
    return @"";
}

+ (NSString *)transErrorInfo:(NSError *)error
{
    NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
    NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    
    return str;
    
}

+(BOOL) runningInBackground
{
    UIApplicationState state = [UIApplication sharedApplication].applicationState;
    BOOL result = (state == UIApplicationStateBackground);
    
    return result;
}

+(BOOL) runningInForeground
{
    UIApplicationState state = [UIApplication sharedApplication].applicationState;
    BOOL result = (state == UIApplicationStateActive);
    
    return result;
}

@end
